﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using ImageToolKit.Net;

namespace ImageInversion
{
    class Program
    {
        static void PrintHelp()
        {
            string help = "ImageInversion.exe dir parrten Rotate:Rotate0,Rotate90,Rotate180,Rotate270,Horizontal,Vertical";
            help += "Rotate0 旋转至正常" + Environment.NewLine;
            help += "Ratate90 顺时针旋转90度" + Environment.NewLine;
            help += "Ratate180 顺时针旋转180度" + Environment.NewLine;
            help += "Ratate270 顺时针旋转270度" + Environment.NewLine;
            help += "Horizontal 图片根据Y轴对称" + Environment.NewLine;
            help += "Vertical 图片根据X轴对称" + Environment.NewLine;
            Console.WriteLine(help);
        }

        public static void Main(string[] args)
        {
            if (args.Length < 3)
            {
                PrintHelp();
                return;
            }
            try
            {

                string[] files = System.IO.Directory.GetFiles(args[0], args[1], System.IO.SearchOption.TopDirectoryOnly);

                if (files.Length == 0)
                {
                    Console.WriteLine("没有找到任何文件，搜索条件:" + args[0] + args[1]);
                    return;
                }

                string strValue = Enum.GetNames(typeof(WICBitmapTransformOptions)).FirstOrDefault(obj => obj.IndexOf(args[2]) >= 0);

                if (strValue == null)
                {
                    Console.WriteLine("参数Rotate值不正确");
                    return;
                }
                WICBitmapTransformOptions value = (WICBitmapTransformOptions)Enum.Parse(typeof(WICBitmapTransformOptions), strValue);

                Console.WriteLine("共找到图片:" + files.Length);
                foreach (string file in files)
                {
                    bool ret = ImageToolKit.Net.ImageToolKitNative.ImageInversion(file, file+".jpg", value);
                    if (ret == true)
                    {
                        File.Delete(file);
                        File.Move(file + ".jpg", file);
                        Console.WriteLine("已成功:" + file);
                    }
                    else
                    {
                        Console.WriteLine("处理过程中止，因为无法处理图片:" + file);
                        return;
                    }
                }

                Console.WriteLine("共处理图片:" + files.Length);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}
